const mongoose = require('mongoose');
//var connect=require('../mongodb')();

var Schema = mongoose.Schema;

//required:必填 validate:自定义匹配函数，返回bool min: 最小值(只适用于数字)max: 最大值(只适用于数字)   
//match: 正则匹配(只适用于字符串) enum:  枚举匹配(只适用于字符串)
var schema = new Schema({
    name:  String,
    pwd:String,
    state:{type:String,default:'0'},
    phone:String,
    email:String,
    type:String,
    source:String,
    dec:String,
    time:String,
    imgurl:String,
    nickname:String,
    contentnumber:{type:Number,default:1},//文章数
    follownumber:{type:Number,default:1},//关注数
    answernumber:{type:Number,default:1},//回答数
});
//静态方法，用mode.来访问
schema.statics._find_name_pwd=function(name,pwd,call){
    return this.find({name:name,pwd:pwd},call)
}
//model 实例方法，new model.findstate
schema.methods.findstate=function(call){
    return this.model('user').find({state:this.state}).exec(call);
}
//给model 添加查询方法   model.find().byname('star')
schema.query.byname=function(name){
    return this.find({name:new RegExp(name,'ig')});
}
schema.query.bystate=function(state){
    return this.find({state:state});
}
var model=mongoose.model('user',schema);
module.exports=model;

